//
//  CustomIMSendMessageView.m
//  txlc_ios
//
//  Created by ayi on 2018/10/17.
//  Copyright © 2018 txlc. All rights reserved.
//

#import "CustomIMSendMessageView.h"

@interface CustomIMSendMessageView()

@property (nonatomic) CGRect cpframe;

@property (nonatomic, strong) UITextField *talkTextfield;
@property (nonatomic, strong) UILabel *countLab;
@property (nonatomic, strong) UIButton *btn1;
@property (nonatomic, strong) UIButton *btn2;

@end

@implementation CustomIMSendMessageView

- (instancetype)initWithFrame:(CGRect)frame
{
    self = [super initWithFrame:frame];
    if (self) {
        if (frame.size.height == 0 || frame.size.width == 0) {
            self.cpframe = CGRectMake(0, BOUNDHEIGHT - TABBAR_HEIGHT - NAV_HEIGHT, BOUNDWIDTH, TABBAR_HEIGHT);
        }else{
            self.cpframe = frame;
        }
        self.frame = self.cpframe;
        self.backgroundColor = UIColor.whiteColor;
        
        [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(keyboardWillShow:) name:UIKeyboardWillShowNotification object:nil];
        [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(keyboardWillHide:) name:UIKeyboardWillHideNotification object:nil];
        
        _talkTextfield = ({
            UITextField *textf = [[UITextField alloc]init];
            textf.font = FONT_SIZE(14);
            textf.textColor = Common_TextColor;
            textf.textAlignment = NSTextAlignmentLeft;
            textf.placeholder = @"写评论...";
            textf;
        });
        
        UIView *talkView = [[UIView alloc] init];
        talkView.backgroundColor = [UIColor clearColor];
        talkView.layer.cornerRadius = 15;
        talkView.layer.masksToBounds = YES;
        talkView.layer.borderColor= Common_TextLowColor.CGColor;
        talkView.layer.borderWidth = 1;
        [self addSubview:talkView];
        talkView.frame = CGRectMake(15, 5, BOUNDWIDTH - 30 - 50, 40);

        _btn1 = ({
            UIButton *btn = [UIButton buttonWithType:UIButtonTypeCustom];
            btn.titleLabel.font = FONT_SIZE(14);
//            [btn addTarget:self action:@selector(clickBtnAction:) forControlEvents:UIControlEventTouchUpInside];
            [btn setImage:IMG(@"pinglun1") forState:UIControlStateNormal];
            [btn setImage:IMG(@"pinglun1") forState:UIControlStateHighlighted];
            btn;
        });
        
        
        UIView *lineview = [[UIView alloc]init];lineview.backgroundColor = Common_SepeColor;
        
        [talkView addSubview:_talkTextfield];
        [self addSubview:_btn1];
        [self addSubview:lineview];
        
        lineview.frame = CGRectMake(0, 0, CGRectGetWidth(self.frame), .5f);
        _btn1.frame = CGRectMake(BOUNDWIDTH - 55, (CGRectGetHeight(self.frame)-40)/2, 40, 40);
        [_btn1 addTarget:self action:@selector(handleSendAction:) forControlEvents:UIControlEventTouchUpInside];

        _talkTextfield.frame = CGRectMake(15, 0, CGRectGetWidth(talkView.frame)- 30, 40);
        
        
        [self addRac];
    }
    return self;
}


- (void)handleSendAction:(id)sender
{
    if (self.CustomIMCallBack) {
        self.CustomIMCallBack(self.talkTextfield);
    }
    
}


- (instancetype)initOnlySendWithFrame:(CGRect)frame
{
    self = [super initWithFrame:frame];
    if (self) {
        if (frame.size.height == 0 || frame.size.width == 0) {
            self.cpframe = CGRectMake(0, BOUNDHEIGHT - TABBAR_HEIGHT - NAV_HEIGHT, BOUNDWIDTH, TABBAR_HEIGHT);
        }else{
            self.cpframe = frame;
        }
        self.frame = self.cpframe;
        self.backgroundColor = UIColor.whiteColor;
        
        [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(keyboardWillShow:) name:UIKeyboardWillShowNotification object:nil];
        [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(keyboardWillHide:) name:UIKeyboardWillHideNotification object:nil];
        
        _talkTextfield = ({
            UITextField *textf = [[UITextField alloc]init];
            textf.font = FONT_SIZE(14);
            textf.textColor = Common_TextColor;
            textf.textAlignment = NSTextAlignmentLeft;
            textf.placeholder = @"请输入";
            textf;
        });
        
        _btn1 = ({
            UIButton *btn = [UIButton buttonWithType:UIButtonTypeCustom];
            [btn addTarget:self action:@selector(sendMesssageAction:) forControlEvents:UIControlEventTouchUpInside];
            btn.backgroundColor = Common_NavigroundColor;
            [btn setTitle:@"发送" forState:UIControlStateNormal];
            [btn setTitleColor:UIColor.whiteColor forState:UIControlStateNormal];
            btn.titleLabel.font = [UIFont systemFontOfSize:14];
            btn.layer.masksToBounds = YES;
            btn.layer.cornerRadius = 4;
            btn;
        });

        UIView *lineview = [[UIView alloc]init];lineview.backgroundColor = Common_SepeColor;
        
        [self addSubview:_talkTextfield];
        [self addSubview:_btn1];
        [self addSubview:lineview];
        
        lineview.frame = CGRectMake(0, 0, CGRectGetWidth(self.frame), .5f);
        _btn1.frame = CGRectMake(CGRectGetWidth(self.frame) - 10 - 60, 7.5, 60, 35);
        _talkTextfield.frame = CGRectMake(15, 10, CGRectGetMinX(_btn1.frame) - 30, 30);
    }
    return self;
}







- (void)setCount:(NSInteger)count{
    _count = count;
}

- (void)addRac{
    @weakify(self);
    
    [[RACObserve(self, count) skip:1]subscribeNext:^(id  _Nullable object) {
        @strongify(self);
        if ([object integerValue] <= 0) {
            self.countLab.hidden = YES;
        }else{
            NSString *str;
            if ([object integerValue] >= 100) {
                str = @"99+";
            }else{
                str = [NSString stringWithFormat:@"%ld",(long)[object integerValue]];
            }
            self.countLab.hidden = NO;
            CGSize size = [CommonMethod getWidthWithStr:str andFont:self.countLab.font andWidth:BOUNDWIDTH];
            size.width = (size.width + 4)<CGRectGetHeight(self.countLab.frame)?CGRectGetHeight(self.countLab.frame):(size.width + 4);
            self.countLab.text = str;
            
            CGRect frame = self.countLab.frame;
            self.countLab.frame = ({
                frame.size.width = size.width;
                frame;
            });
        }
    }];
    
}

- (void)clickBtnAction:(UIButton *)btn{
    
}


- (void)sendMesssageAction:(UIButton *)btn{
    if (self.CustomIMCallBack) {
        self.CustomIMCallBack(self.talkTextfield);
    }
}

- (void)becomeFirstResponder{
    [self.talkTextfield becomeFirstResponder];
}

- (void)sendSuccess{
    self.talkTextfield.text = @"";
    [self.talkTextfield resignFirstResponder];
}


//当键盘出现
- (void)keyboardWillShow:(NSNotification *)notification
{
    //获取键盘的高度
    NSDictionary *userInfo = [notification userInfo];
    NSValue *value = [userInfo objectForKey:UIKeyboardFrameEndUserInfoKey];
    CGRect keyboardRect = [value CGRectValue];
    CGFloat height = keyboardRect.size.height;
    CGRect oneframe = self.cpframe;
    oneframe.origin.y = BOUNDHEIGHT - self.superview.frame.origin.y - height - self.cpframe.size.height;
    [UIView animateWithDuration:0.35 animations:^{
        self.frame = oneframe;
    } completion:^(BOOL finished) {
        
    }];
}

//当键退出
- (void)keyboardWillHide:(NSNotification *)notification
{
    [UIView animateWithDuration:0.35 animations:^{
        self.frame = self.cpframe;
    } completion:^(BOOL finished) {
        
    }];
}


@end
